package com.hust.bridgecheck.controller.UserManagement;

import com.hust.bridgecheck.entity.User;
import com.hust.bridgecheck.service.UserManagement.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    private String addUser(User user) {
        userService.addUser(user);
        return "redirect:/user/manager";
    }

    @DeleteMapping("/delete/{id}")
    private String deleteUser(@PathVariable("id") Integer userID) {
        userService.deleteUser(userID);
        return "redirect:/user/manager";
    }

    @GetMapping("/all")
    @ResponseBody
    private List<User> getAllUser() {

        return userService.getAllUser();
    }

    @GetMapping("/getbyid")
    @ResponseBody
    public User getUserByID( Integer userID) {
        System.out.println(userID);
        User user = userService.getUserByID(userID);
        return user;
    }


    @PutMapping("/updateuser")
    private String updateUser(User user) {
        userService.updateUser(user);
        return "redirect:/user/manager";
    }

    @PostMapping("/generateuser")
    @ResponseBody
    private void generateUser(String userNum,String groupId){
        userService.generateUser(Integer.valueOf(userNum),Integer.valueOf(groupId));
    }
}



